package com.fortune.controller;

import java.util.List;
import java.util.UUID;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

import com.fortune.base.BaseController;
import com.fortune.dto.excution.ReturnDepositExcution;
import com.fortune.enums.ReturnDepositEnum;
import com.fortune.service.ReturnDepositService;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.returnDeposit.AddDto;
import com.fortune.dto.in.returnDeposit.EditDto;
import com.fortune.vo.returnDeposit.ReturnDepositInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "wechat/returnDeposit", description = "工单相关", tags = {"微信小程序（业务员）：工单管理"})
@RestController
@RequestMapping("wechat/returnDeposit")
public class ReturnDepositController extends BaseController{
	@Autowired
	private ReturnDepositService returnDepositService;

	@ApiOperation(value = "工单列表", nickname = "returnDepositList", notes = "", tags={ "@郭阳" })

	@ApiImplicitParams({
		@ApiImplicitParam(name="type", value="类型（1未处理2已批准3已接货4已完成）",paramType="query",dataType="int",allowableValues="1,2,3,4"),
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<ReturnDepositInfo>> list(@Validated @Min(value=1) @Max(value=4) int type) {
		String mid = getMId();
		String did = getDId();
		String userId = getUserId();
		if(type == 1){
			List<ReturnDepositInfo> findReturnDepositInfoByUn = returnDepositService.findReturnDepositInfoByUn(mid, did, userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByUn, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 2){
			List<ReturnDepositInfo> findReturnDepositInfoByApproved = returnDepositService.findReturnDepositInfoByApproved(mid,did,userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByApproved, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 3){
			List<ReturnDepositInfo> findReturnDepositInfoByReceived = returnDepositService.findReturnDepositInfoByReceived(mid,did,userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByReceived, ReturnDepositEnum.FIND_SUCCESS);
		}
		if(type == 4){
			List<ReturnDepositInfo> findReturnDepositInfoByCompleted = returnDepositService.findReturnDepositInfoByCompleted(mid, did, userId);
			return new ReturnDepositExcution<List<ReturnDepositInfo>>(findReturnDepositInfoByCompleted, ReturnDepositEnum.FIND_SUCCESS);
		}
		return new ReturnDepositExcution<List<ReturnDepositInfo>>(null, ReturnDepositEnum.FIND_ERROR);
	}
	
	@ApiOperation(value = "添加工单", nickname = "returnDepositAdd", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody AddDto addDto) {
		String mid = getMId();
		String did = getDId();
		String crdid = UUID.randomUUID().toString().replace("-","");//工单id
		int addReturnDeposit = returnDepositService.addReturnDeposit(addDto, mid, did, crdid);
		if (addReturnDeposit < 1) {
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.ADD_ERROR);
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.ADD_SUCCESS);
	}
	
	@ApiOperation(value = "编辑工单", nickname = "returnDepositEdit", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody EditDto editDto) {
		String mid = getMId();
		String did = getDId();
		int editReturnDeposit = returnDepositService.editReturnDeposit(editDto, mid, did);
		if (editReturnDeposit < 1) {
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_ERROR);
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.EDIT_SUCCESS);
	}
	
	@ApiOperation(value = "删除工单", nickname = "returnDepositDelete", notes = "", tags={ "@郭阳" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@ApiImplicitParam(name="id", value="工单id",paramType="query",dataType="string")
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotBlank(message="工单id不能为空") String id) {
		int deleteReturnDeposit = returnDepositService.deleteReturnDeposit(id);
		if(deleteReturnDeposit <1){
			return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.DELETE_ERROR);
		}
		return new ReturnDepositExcution<Void>(null, ReturnDepositEnum.DELETE_SUCCESS);
	}
}
